package com.goods.business.service.imp;

import com.goods.business.mapper.BizProductMapper;
import com.goods.business.mapper.BizProductStockMapper;
import com.goods.business.service.BizProductService;
import com.goods.common.model.business.BizProduct;
import com.goods.common.model.business.BizProductStock;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class BizProductServiceImpl implements BizProductService {

    @Autowired
    private BizProductMapper bizProductMapper;
    @Autowired
    private BizProductStockMapper bizProductStockMapper;

    // TODO: 2021/9/27  如果出单没有消费者，前台查询不到明细星系
    @Override
    public PageVO<ProductVO> findProductList(Map<String,Object> queryMap) {
        //创建查询实例
        Example example = new Example(BizProduct.class);
        //获取参数
        Example.Criteria criteria = example.createCriteria();
        if (queryMap.containsKey("name") && !queryMap.get("name").equals(""))
            criteria.andLike("name",(String)queryMap.get("name"));
        //检查时候包含分类ids
        if (queryMap.containsKey("categorys") && !queryMap.get("categorys").equals("")) {
            String categorys = (String) queryMap.get("categorys");
            String[] split = categorys.split(",");//截取数组
            int length = split.length;//获取数组长度
            switch (length){
                case 1: criteria.andEqualTo("oneCategoryId", split[0]);//一级分类
                    break;
                case 2: criteria.andEqualTo("twoCategoryId", split[1]);//二级分类
                    break;
                case 3: criteria.andEqualTo("threeCategoryId", split[2]);//三级分类
                    break;
            }
        }
        if (queryMap.containsKey("supplier") && !queryMap.get("supplier").equals(""))
            criteria.andEqualTo("supplier", queryMap.get("supplier"));
        if (queryMap.containsKey("status"))
            criteria.andEqualTo("status", queryMap.get("status"));//逻辑删除

        List<BizProduct> bizProducts = bizProductMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(bizProducts)){
            return new PageVO<>(0, null);
        }
        List<ProductVO> productVOS = new ArrayList<>();
        for (BizProduct bizProduct : bizProducts) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(bizProduct, productVO);
            productVOS.add(productVO);
        }
        List<ProductVO> page = null;
        if (queryMap.get("pageNum") != null && queryMap.get("pageSize") != null) {
            page = ListPageUtils.page(productVOS, Integer.parseInt((String) queryMap.get("pageSize")), Integer.parseInt((String) queryMap.get("pageNum")));
            return new PageVO<>(bizProducts.size(),page);
        }
        return new PageVO<>(bizProducts.size(),productVOS);
    }

    @Override
    public void addProduct(ProductVO productVO) {
        BizProduct bizProduct = new BizProduct();
        BeanUtils.copyProperties(productVO, bizProduct);
        // TODO: 2021/9/24 未添加物资编号
        Long[] categoryKeys = productVO.getCategoryKeys();
        if (categoryKeys != null) {
            //获取分类数据
            String[] split = Arrays.toString(categoryKeys).split(",");
            int length = split.length;
            switch (length){
                case 1 : bizProduct.setOneCategoryId(Long.parseLong(split[0]));
                    break;
                case 2 : bizProduct.setTwoCategoryId(Long.parseLong(split[1]));
                    break;
                case 3 : bizProduct.setThreeCategoryId(Long.parseLong(split[2]));
                    break;
            }
        }
        bizProduct.setStatus(2);//物资进入审核状态
        bizProductMapper.insert(bizProduct);
    }

    @Override
    public BizProduct getProductById(Integer id) {
        return bizProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateBizProduct(Integer id, ProductVO productVO) {
        BizProduct bizProduct = bizProductMapper.selectByPrimaryKey(id);
        if (bizProduct!=null){
            BizProduct bizProduct1 = new BizProduct();
            BeanUtils.copyProperties(productVO, bizProduct1);
            bizProduct1.setId(bizProduct.getId());
            bizProductMapper.updateByPrimaryKey(bizProduct1);
        }
    }

    @Override
    public void updateProductStatus(Integer id, Integer status) {
        BizProduct bizProduct = bizProductMapper.selectByPrimaryKey(id);
        bizProduct.setStatus(status);
        bizProductMapper.updateByPrimaryKey(bizProduct);
    }


    @Override
    public void deleteProductById(Integer id) {
        bizProductMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageVO<ProductStockVO> findProductStocks(Map<String, Object> queryMap) {
        Example example = new Example(BizProduct.class);
        //设置条件
        if (queryMap.containsKey("name") && !queryMap.get("name").equals(""))
            example.createCriteria().andLike("name", (String) queryMap.get("name"));
        if (queryMap.containsKey("status"))
            example.createCriteria().andLike("status", (String) queryMap.get("status"));
        //检查时候包含分类ids
        if (queryMap.containsKey("categorys") && !queryMap.get("categorys").equals("")) {
            String categorys = (String) queryMap.get("categorys");
            String[] split = categorys.split(",");//截取数组
            int length = split.length;//获取数组长度
            switch (length){
                case 1: example.createCriteria().andEqualTo("oneCategoryId", split[0]);//一级分类
                    break;
                case 2: example.createCriteria().andEqualTo("twoCategoryId", split[1]);//二级分类
                    break;
                case 3: example.createCriteria().andEqualTo("threeCategoryId", split[2]);//三级分类
                    break;
            }
        }
        example.createCriteria().andEqualTo("status", 0);
        //查询商品
        List<BizProduct> bizProducts = bizProductMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(bizProducts))
            return new PageVO<>(0, null); //没有数据，直接返回空值
        //查询库存
        List<ProductStockVO> productStockVOS = new ArrayList<>();
        for (BizProduct bizProduct : bizProducts) {
            Example example1 = new Example(BizProductStock.class);
            example1.createCriteria().andEqualTo("pNum", bizProduct.getpNum());
            BizProductStock bizProductStock = bizProductStockMapper.selectOneByExample(example1);
            example1.clear();
            ProductStockVO productStockVO = new ProductStockVO();
            this.packageBizProduct(productStockVO, bizProduct, bizProductStock);
            productStockVOS.add(productStockVO);
        }
        if (queryMap.get("pageNum") !=null && queryMap.get("pageSize") != null){
            ListPageUtils.page(productStockVOS, Integer.parseInt((String) queryMap.get("pageNum")), Integer.parseInt((String) queryMap.get("pageSize")));
        }
        PageVO<ProductStockVO> productStockVOPageVO = new PageVO<>(bizProducts.size(), productStockVOS);
        return productStockVOPageVO;
    }

    @Override
    public List<ProductStockVO> findAllStocks() {
        Example example = new Example(BizProduct.class);
        List<BizProduct> bizProducts = bizProductMapper.selectByExample(example);
        List<ProductStockVO> productStockVOS = new ArrayList<>();
        for (BizProduct bizProduct : bizProducts) {
            Example example1 = new Example(BizProductStock.class);
            example1.createCriteria().andEqualTo("pNum", bizProduct.getpNum());
            BizProductStock bizProductStock = bizProductStockMapper.selectOneByExample(example1);
            example1.clear();
            ProductStockVO productStockVO = new ProductStockVO();
            this.packageBizProduct(productStockVO, bizProduct, bizProductStock);
            productStockVOS.add(productStockVO);
        }
        return productStockVOS;
    }

    private void packageBizProduct(ProductStockVO productStockVO, BizProduct bizProduct, BizProductStock bizProductStock) {
        productStockVO.setId(bizProduct.getId());
        productStockVO.setName(bizProduct.getName());
        productStockVO.setPNum(bizProduct.getpNum());
        productStockVO.setModel(bizProduct.getModel());
        productStockVO.setUnit(bizProduct.getUnit());
        productStockVO.setRemark(bizProduct.getRemark());
        productStockVO.setStock(bizProductStock.getStock());
        productStockVO.setImageUrl(bizProduct.getImageUrl());
    }
}
